package manager.rank;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import database.pojo.SingleSongScoreInfo;
import database.pojo.UserInfo;
import javassist.expr.NewArray;
import manager.rank.type.singSongNumInfo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import util.MyFactory;
import util.RedisUtil;

public class QuireRankOfSong {
	private Jedis myJedis =null;
	
	/**
	 * 获取单曲得分排名列表
	 * @param songId
	 * @param number
	 * @param rankRange
	 * @return
	 */
	public ArrayList<SingleSongScoreInfo> getSingleSongScoreRankInfo(String songId, int number, int rankRange) {
		ArrayList<SingleSongScoreInfo> sList =new ArrayList<SingleSongScoreInfo>();
		SingleSongScoreInfo scoreInfo =null;
		Set<Tuple> redisInfo =null;
		int num =0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange == 0) {
				redisInfo=myJedis.zrevrangeByScoreWithScores("vod:SingleSongScoreRank:"+songId,"+inf","-inf",0,number);
			}else {
				redisInfo=myJedis.zrevrangeByScoreWithScores("vod:SingleSongScoreRank:"+songId+":"+rankRange,"+inf","-inf",0,number);
			}
			for(Tuple redisInfoTuple:redisInfo) {
				num =num +1;
				scoreInfo = new SingleSongScoreInfo();
				scoreInfo.setOpenId(redisInfoTuple.getElement());
				scoreInfo.setScore((int)redisInfoTuple.getScore());
				scoreInfo.setRank(num);
				System.out.println("getSingleSongScoreRankInfo name:"+redisInfoTuple.getElement()+" score:"+redisInfoTuple.getScore());
				sList.add(scoreInfo);
			}
		}catch (Exception e) {
			System.out.println("getSingleSongScoreRankInfo get a Exception---"+e);
			return sList;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池			
		}
		return sList;
		
	}

	/**
	 * 获取这个openid的排名和分数
	 * @param openId
	 * @param songId
	 * @param rankRange
	 * @return
	 */
	public Map<Integer, Integer> getSingleSongScoreRankForTheOpenId(String openId, String songId, int rankRange) {
		int rank =0;
		int score =0;
		Map<Integer, Integer> rankMap = new HashMap<Integer, Integer>();
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange ==0) {
				rank =myJedis.zrevrank("vod:SingleSongScoreRank:"+songId, openId).intValue()+1;
				score = myJedis.zscore("vod:SingleSongScoreRank:"+songId, openId).intValue();
			}else {
				rank =myJedis.zrevrank("vod:SingleSongScoreRank:"+songId+":"+rankRange, openId).intValue()+1;
				score = myJedis.zscore("vod:SingleSongScoreRank:"+songId+":"+rankRange, openId).intValue();
			}
		}catch (Exception e) {
			System.out.println("getSingleSongScoreRankForTheOpenId get a Exception---"+e);
			return rankMap;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			rankMap.put(rank, score);
			
		}
		return rankMap;
	}

	/**
	 * 获取唱歌次数排名的列表
	 * @param openId
	 * @param number
	 * @param rankRange
	 * @return
	 */
	public ArrayList<singSongNumInfo> getSingSongNumRankList(String openId, int number, int rankRange) {
		ArrayList<singSongNumInfo> sList =new ArrayList<singSongNumInfo>();
		singSongNumInfo scoreInfo =null;
		Set<Tuple> redisInfo =null;
		//Tuple redisInfoTuple =null;
		int num =0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange == 0) {
				redisInfo=myJedis.zrevrangeByScoreWithScores("vod:SongNumRank","+inf","-inf",0,number);
			}else {
				redisInfo=myJedis.zrevrangeByScoreWithScores("vod:SongNumRank:"+rankRange,"+inf","-inf",0,number);
			}
			//System.out.println("redisInfo size:"+redisInfo.size());
			for(Tuple redisInfoTuple:redisInfo) {
				num =num +1;
				scoreInfo = new singSongNumInfo();
				scoreInfo.setOpenId(redisInfoTuple.getElement());
				scoreInfo.setNumber((int)redisInfoTuple.getScore());
				scoreInfo.setRank(num);
				//System.out.println("getsingSongNumInfo name:"+redisInfoTuple.getElement()+" score:"+redisInfoTuple.getScore());
				sList.add(scoreInfo);
			}
			
		}catch (Exception e) {
			System.out.println("getsingSongNumInfo get a Exception---"+e);
			return sList;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池			
		}
		return sList;
	}

	/**
	 * 获取这个openid唱歌次数和排名
	 * @param openId
	 * @param rankRange
	 * @return
	 */
	public  Map<Integer, Integer> getSingSongNumRankForPeople(String openId, int rankRange) {
		int rank =0;
		int score =0;
		Map<Integer, Integer> rankMap = new HashMap<Integer, Integer>();
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange ==0) {
				if(myJedis.sismember("vod:SongNumRank", openId)) {
					rank =myJedis.zrevrank("vod:SongNumRank", openId).intValue()+1;
					score = myJedis.zscore("vod:SongNumRank", openId).intValue();
				}
			}else {
				if(myJedis.sismember("vod:SongNumRank:"+rankRange, openId)) {
					rank =myJedis.zrevrank("vod:SongNumRank:"+rankRange, openId).intValue()+1;
					score = myJedis.zscore("vod:SongNumRank:"+rankRange, openId).intValue();
				}
			}
		}catch (Exception e) {
			System.out.println("getSingSongNumRankForPeople get a Exception---"+e);
			return rankMap;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			rankMap.put(rank, score);			
		}
		return rankMap;
	}
	/**
	 * 获取单曲评分榜上的总人数
	 * @param songId
	 * @param rankRange
	 * @return
	 */
	public int getTotalNumOfSongId(String songId, int rankRange) {
		int num =0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange ==0) {
				num = myJedis.zcard("vod:SingleSongScoreRank:"+songId).intValue();
			}else {
				num = myJedis.zcard("vod:SingleSongScoreRank:"+songId+":"+rankRange).intValue();
			}
		}catch (Exception e) {
			System.out.println("getTotalNumOfSongIdRedis get a Exception---"+e);
			return num;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			
		}
		return num;
	}

	/**
	 * 获取唱不同歌曲的总人数：
	 * @param openId
	 * @param rankRange
	 * @return
	 */
	public int getTotalNumOfOpenId(int rankRange) {
		int num =0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			if(rankRange ==0) {
				num = myJedis.zcard("vod:SongNumRank").intValue();
			}else {
				num = myJedis.zcard("vod:SongNumRank:"+rankRange).intValue();
			}
		}catch (Exception e) {
			System.out.println("getTotalNumOfSongIdRedis get a Exception---"+e);
			return num;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			
		}
		return num;
	}
	
	
	/**
	 * 获取唱过的歌曲set
	 * @param openId
	 * @return songSet
	 */
	public Set<String> getSongIdSet(String openId) {
		Set<String> songSet = null;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			songSet = myJedis.smembers("vod:SingSongSet:"+openId);
		}catch (Exception e) {
			System.out.println("getSongIdSet get a Exception---"+e);
			return songSet;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			
		}
		return songSet;
	}
	
	
	public int  getSongScoreOfOpenId(String songId,String openId) {
		int score = 0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			score =  myJedis.zscore("vod:SingleSongScoreRank:"+songId, openId).intValue();
		}catch (Exception e) {
			System.out.println("getSongScoreOfOpenId get a Exception---"+e);
			return score;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池
			
		}
		return score;
	}
	
	public int  getSongRankOfOpenId(String songId,String openId) {
		int rank = 0;
		try {
			myJedis=MyFactory.context.getBean("RedisUtil", RedisUtil.class).getJedis();
			rank = myJedis.zrevrank("vod:SingleSongScoreRank:"+songId, openId).intValue() ;
			if((Integer)rank!=null) {
				rank++;
			}else {
				rank =0;
			}
		}catch (Exception e) {
			System.out.println("getSongRankOfOpenId get a Exception---"+e);
			return 0;
		}finally {
			if(null != myJedis)
				myJedis.close(); // 释放资源还给连接池		
		}
		return rank;
		
	}

}
